#include "effects.h"
#include "video.h"
#include "sound.h"
#include "GL/glfw.h"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cmath>

/*==============================================================================
								Effect class
==============================================================================*/

Effect::Effect(Video* video, Sound* sound)
	: video(video), sound(sound), sound_db(0.0f), sound_freq(0.0f), 
	sound_freq_octave(0), sound_spectrum(NULL), time_now(0.0f)
{
	// Initializes sound if in hadn't been done yet
	if (sound->IsInit() == false)
	{
		sound->Init();
		sound->Run();
	}
}

Effect::~Effect()
{
	// Closes sound if it is not closed
	if (sound->IsClosed() == false)
		sound->Close();
}

// Rewrite this in your own class to render effect
void Effect::Render()
{
}

// Virtual update function. Rewrite, just add SystemUpdate()
void Effect::Update()
{
	SystemUpdate();
}

// Gives main frequency of recorded sound
float Effect::GetSoundFreq()
{
	return sound_freq;
}

// Gives sound intensity in decibels
float Effect::GetSoundDB()
{
	return sound_db;
}

// Gives current sound note octave
int Effect::GetSoundOctave()
{
	return sound_freq_octave;
}

// Gives sound spectrum in array of floats
float* Effect::GetSpectrum()
{
	return sound_spectrum;
}

// Returns entry hz
float Effect::GetEntryHz()
{
	return entry_hz;
}

// Gives current time
float Effect::GetTime()
{
	return time_now;
}

// Gives a note name
std::string Effect::GetNote()
{
	return note;
}

// Updates system: sound, video, time
void Effect::SystemUpdate()
{
	time_now = (float)glfwGetTime();
	video->Render();
	sound->Update();
	sound_freq = sound->GetFreq("BASS");
	sound_db = sound->GetDB("BASS");
	sound_freq_octave = sound->GetOctave();
	sound_spectrum = sound->GetSpectrum();
	entry_hz = sound->GetEntryHz();
	note = sound->GetNote();
}
